package com.kingdee.eas.jky.crm.app;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.bot.BOTRelationFactory;
import com.kingdee.bos.metadata.bot.BOTRelationInfo;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.codingrule.CodingRuleManagerFactory;
import com.kingdee.eas.base.codingrule.ICodingRuleManager;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.DefineProjectFactory;
import com.kingdee.eas.custom.DefineProjectInfo;
import com.kingdee.eas.custom.IDefineProject;
import com.kingdee.eas.custom.crm.BusinessManageFactory;
import com.kingdee.eas.custom.crm.BusinessManageInfo;
import com.kingdee.eas.custom.crm.IBusinessManage;
import com.kingdee.eas.jky.crm.ContractChangeCollection;
import com.kingdee.eas.jky.crm.ContractChangeEntryInfo;
import com.kingdee.eas.jky.crm.ContractChangeFactory;
import com.kingdee.eas.jky.crm.ContractChangeInfo;
import com.kingdee.eas.jky.crm.ContractGudCollection;
import com.kingdee.eas.jky.crm.ContractGudEntryCollection;
import com.kingdee.eas.jky.crm.ContractGudEntryInfo;
import com.kingdee.eas.jky.crm.ContractGudFactory;
import com.kingdee.eas.jky.crm.ContractGudInfo;
import com.kingdee.eas.jky.crm.ContractManageFactory;
import com.kingdee.eas.jky.crm.ContractManageInfo;
import com.kingdee.eas.jky.crm.IContractChange;
import com.kingdee.eas.jky.crm.IContractGud;
import com.kingdee.eas.jky.crm.IContractManage;
import com.kingdee.eas.jky.enums.BusinEnum;
import com.kingdee.eas.jky.enums.ContractEnum;
import com.kingdee.eas.jky.enums.GdStateEnum;
import com.kingdee.eas.jky.pm.IProjectBudget;
import com.kingdee.eas.jky.pm.ProjectBudgetFactory;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.eas.xr.app.XRBillStatusEnum;

public class ContractGudControllerBean extends AbstractContractGudControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.jky.crm.app.ContractGudControllerBean");
    protected void _audit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._audit(ctx, pk);
         IDefineProject   project =DefineProjectFactory.getLocalInstance(ctx);
         IContractManage   contract =ContractManageFactory.getLocalInstance(ctx);
         IContractGud   ContractGud =ContractGudFactory.getLocalInstance(ctx);
         IContractChange   contractChange =ContractChangeFactory.getLocalInstance(ctx);
         IBusinessManage buss =BusinessManageFactory.getLocalInstance(ctx);
         ContractGudInfo info = (ContractGudInfo) this.getValue(ctx, pk);
         info.setGdState(GdStateEnum.ygd);
         info.setContractState(ContractEnum.sign);
         info.setGdDate(new Timestamp(System.currentTimeMillis()));
         ContractGud.update(new ObjectUuidPK(info.getId()), info);
         if(info.getSourceBillId()!=null){
        	 ContractManageInfo contactInfo = contract.getContractManageInfo(new ObjectUuidPK(info.getSourceBillId()));
        	 contactInfo.setGdState(GdStateEnum.ygd);
        	 contactInfo.setContractState(ContractEnum.sign);
        	 contactInfo.setGdDate(new Timestamp(System.currentTimeMillis()));
        	 contract.update(new ObjectUuidPK(contactInfo.getId()), contactInfo);
        	 if(info.getBusiness()!=null){
    			 BusinessManageInfo bInfo = buss.getBusinessManageInfo(new ObjectUuidPK(info.getBusiness().getId()));
    			 bInfo.setContactNumber(contactInfo);
    			 bInfo.setBusinType(BusinEnum.winbill);
    			 buss.update(new ObjectUuidPK(info.getBusiness().getId()), bInfo);
    		 }
         }
         
         if(info.getProjectName()!=null){
        	 String projectID= info.getProjectName().getId().toString();
			 DefineProjectInfo projectInfo =project.getDefineProjectInfo(new ObjectUuidPK(projectID));
			 projectInfo.setContratName(info.getName());
			 projectInfo.setLinkPhone(info.getLinkPhone());
			 projectInfo.setContratAmount(info.getMoney());
			 projectInfo.setContRateAmount(info.getTaxToPaid());
			 projectInfo.setConTotalAmount(info.getTaxSum());
			 projectInfo.setContratState(ContractEnum.sign);
			 if (info.getCusttomer() != null) {
				 projectInfo.setCustomer(info.getCusttomer());
			}
			 if (info.getLinkMan() != null) {
				 projectInfo.setLinkMan(info.getLinkMan());
			}
			 project.update(new ObjectUuidPK(projectID), projectInfo);
         }
    }
    protected void _unAudit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pk);
         IDefineProject   project =DefineProjectFactory.getLocalInstance(ctx);
         IContractManage   contract =ContractManageFactory.getLocalInstance(ctx);
         IContractGud   ContractGud =ContractGudFactory.getLocalInstance(ctx);
         IBusinessManage buss =BusinessManageFactory.getLocalInstance(ctx);
         ContractGudInfo info = (ContractGudInfo) this.getValue(ctx, pk);
         info.setGdState(GdStateEnum.wgd);
         info.setContractState(ContractEnum.audit);
         info.setGdDate(null);
         ContractGud.update(new ObjectUuidPK(info.getId()), info);
         if(info.getSourceBillId()!=null){
        	 ContractManageInfo contactInfo = contract.getContractManageInfo(new ObjectUuidPK(info.getSourceBillId()));
        	 contactInfo.setGdState(GdStateEnum.wgd);
        	 contactInfo.setContractState(ContractEnum.audit);
        	 contactInfo.setGdDate(null);
        	 contract.update(new ObjectUuidPK(contactInfo.getId()), contactInfo);
        	 if(info.getBusiness()!=null){
     			 BusinessManageInfo bInfo = buss.getBusinessManageInfo(new ObjectUuidPK(info.getBusiness().getId()));
     			 bInfo.setContactNumber(null);
     			 bInfo.setBusinType(BusinEnum.executing);
     			 buss.update(new ObjectUuidPK(info.getBusiness().getId()), bInfo);
     		 }
         }
         if(info.getProjectName()!=null){
        	 String projectID= info.getProjectName().getId().toString();
			 DefineProjectInfo projectInfo =project.getDefineProjectInfo(new ObjectUuidPK(projectID));
			 projectInfo.setContratName(null);
			 projectInfo.setContratAmount(BigDecimal.ZERO);
			 projectInfo.setContRateAmount(BigDecimal.ZERO);
			 projectInfo.setConTotalAmount(BigDecimal.ZERO);
			 projectInfo.setContratState(ContractEnum.audit);
			 project.update(new ObjectUuidPK(projectID), projectInfo);
         }
    }
    protected void _unAudit(Context ctx, IObjectPK[] pks)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pks);
    }
    protected void _backEdit(Context ctx, IObjectValue model)throws BOSException
    {
    }
}